<template>
    <div class="content_panel project_modules">
        <div class="content_panel_top">
            <h3 class="panel_title">
                <span>{{$t('finance.hasRepayList')}}</span>
            </h3>
            <el-form ref="form" :model="form" :rules="rules" :inline="false" class="module_form">
                <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item prop="userId" label="">
                            <el-input v-model="form.userId" placeholder="UserId" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <!--<el-col :span="6">-->
                        <!--<el-form-item prop="repayBankAccount" label="">-->
                            <!--<el-input v-model="form.repayBankAccount" :placeholder="$t('common.repayAccount')" clearable>&lt;!&ndash;还款账号&ndash;&gt;</el-input>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <el-col :span="4">
                        <el-form-item prop="userPhone" label="">
                            <el-input v-model="form.userPhone" :placeholder="$t('common.mobileNumber')" clearable><!--手机号码--></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item prop="orderId" label="">
                            <el-input v-model="form.orderId" :placeholder="$t('common.innerOrderId')" clearable><!--内部订单号--></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item prop="appName" label="">
                            <select-app-list v-model="form.appName" :placeholder="$t('system.appName')"></select-app-list><!--app名称-->
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item prop="contractNumber" label="">
                            <el-input v-model="form.contractNumber" :placeholder="$t('common.repaySerialNumber')" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item prop="status" label="">
                            <el-select v-model="form.status" clearable :placeholder="$t('common.overdueStatus')" style="width: 100%"><!--逾期状态-->
                                <el-option key="2" :label="$t('common.hasOverdue')" value="2"></el-option>
                                <el-option key="3" :label="$t('common.notOverdue')" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item prop="channelCompany" label="">
                            <el-select v-model="form.channelCompany" @change="getChannelPayWay('repayChannel')" clearable :placeholder="$t('common.payChannel')" style="width: 100%"><!--支付渠道-->
                                <el-option v-for="item in payWayList" :key="item.nameCode" :label="item.name" :value="item.nameCode + ''"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4" >
                        <el-form-item prop="repayChannel" label="">
                            <el-select v-model="form.repayChannel" clearable :placeholder="$t('common.payMethod')" style="width: 100%"><!--交易方式-->
                                <el-option v-for="item in payChannelList" :key="item.nameCode" :label="item.name" :value="item.nameCode + ''"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="actualRepaytime" label=""><!--还款时间-->
                            <el-date-picker
                                    style="width:100%"
                                    v-model="form.actualRepaytime"
                                    type="datetimerange"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    value-format="timestamp"
                                    range-separator="-"
                                    :default-time="['00:00:00', '23:59:59']"
                                    :start-placeholder="$t('common.repayStartTime')"
                                    :end-placeholder="$t('common.repayEndTime')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="applyTime" label=""><!--申请时间-->
                            <el-date-picker
                                    style="width:100%"
                                    v-model="form.applyTime"
                                    type="datetimerange"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    value-format="timestamp"
                                    range-separator="-"
                                    :default-time="['00:00:00', '23:59:59']"
                                    :start-placeholder="$t('common.applyStartTime')"
                                    :end-placeholder="$t('common.applyEndTime')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <!-- 伪名 -->
                    <el-col :span="4">
                        <el-form-item prop="whiteProductName" v-if="domain == 'Bene'"> <!---->
                            <el-input v-model="form.whiteProductName" :placeholder="$t('common.productName')" clearable><!----></el-input>
                        </el-form-item>
                        <el-form-item prop="kana" v-else> <!--姓名-->
                            <select-kana-list v-model="form.kana" :multiple="false" :placeholder="$t('finance.pseudonym')"></select-kana-list><!--app名称-->
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <!--放款类型-->
                    <el-col :span="4">
                        <el-form-item prop="repayType" label="">
                            <el-select v-model="form.repayType" :placeholder="$t('common.repaymentType')" clearable style="width: 100%">
                                <el-option v-for="item in keyMap.repaymentType" :key="item.value" :value="item.value" :label="item.label"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button @click="search" icon="el-icon-search" type="primary">{{ $t('common.search') }}</el-button>
                            <el-button @click="exportExcel" icon="el-icon-upload2" type="primary">{{ $t('common.export') }}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form><!--el-form end-->
        </div><!--content_panel_top-->
        <div class="module_content">
            <div class="content_panel project_modules">
                <div class="table_list">
                    <el-table :data="tableData" style="width: 100%;" :highlight-current-row="true" :max-height="tableMaxHeight">
                        <el-table-column label="UserId" prop="userId" width="140"></el-table-column>
                        <el-table-column :label="$t('common.name')"  prop="userName" min-width="150"></el-table-column><!--姓名-->
                        <el-table-column :label="$t('common.mobileNumber')" prop="userPhone" width="150"></el-table-column><!--手机号码-->
                        <el-table-column :label="$t('common.innerOrderId')" prop="orderId" width="200"></el-table-column><!--内部订单号-->
                        <el-table-column :label="$t('system.appName')" prop="appName" min-width="130"></el-table-column><!--APP名称-->
                        <el-table-column v-if="domain=='Bene'" :label="$t('finance.productName')" prop="whiteProductName" min-width="130"></el-table-column><!--伪名-->
                        <el-table-column v-else :label="$t('finance.pseudonym')" prop="kana" min-width="130"></el-table-column><!--伪名-->

                        <el-table-column :label="$t('common.paymentId')" prop="thirdPartyOrderId" width="200"></el-table-column><!--支付ID-->
                        <el-table-column :label="$t('finance.repayChannel')" prop="channelCompany" width="120"><!--还款渠道-->
                            <template slot-scope="{ row }">{{ row.channelCompany | SwitchChannelCompany }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.repayType')" prop="repayChannel" width="150"><!--还款方式-->
                            <template slot-scope="{ row }">{{ row.repayChannel | SwitchLoanChannel }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.repaySerialNumber')" prop="contractNumber" min-width="170"><!--还款序列号--></el-table-column>
                        <el-table-column :label="$t('common.contractAmount')" prop="contractAmount" width="100">
                            <template slot-scope="{ row }">{{ row.contractAmount | formatCurrency }}</template><!--合同金额-->
                        </el-table-column>
                        <el-table-column :label="$t('common.borrowingCycle')" prop="loanPeriod" width="100">
                            <template slot-scope="{ row }">{{ row.loanPeriod }}{{ $t('common.day') }}</template><!--借款周期-->
                        </el-table-column>
                        <el-table-column :label="$t('common.loanAmount')" prop="loanAmount" width="100">
                            <template slot-scope="{ row }">{{ row.loanAmount | formatCurrency }}</template><!--放款金额-->
                        </el-table-column>
                        <el-table-column :label="$t('common.shouldRepayPrincipalAndInterest')" prop="lateRepay" width="100"><!--应还本息-->
                            <template slot-scope="{ row }">{{ row.lateRepay | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.restRepay')" prop="restRepay" width="100"><!--挂账金额-->
                            <template slot-scope="{ row }">{{ row.restRepay | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.actRepayAmount')" prop="payedAmount" width="100">
                            <template slot-scope="{ row }">{{ row.payedAmount | formatCurrency }}</template>
                        </el-table-column><!--实际还款金额-->
                        <el-table-column :label="$t('common.coupons')" prop="ticketRepayAmount" width="100">
                            <template slot-scope="{ row }">{{ row.ticketRepayAmount > 0 ? row.ticketRepayAmount : '--' }}</template>
                        </el-table-column><!--优惠券-->
                        <el-table-column :label="$t('common.repayFee')" prop="repayFee" width="100"><!--还款手续费-->
                            <template slot-scope="{ row }">{{ row.repayFee | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.addFee')" prop="tax" width="100"><!--增值税-->
                            <template slot-scope="{ row }">{{ row.tax | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.actualCreditAmount')" prop="repayFee" width="120"><!--实际入账金额-->
                            <template slot-scope="{ row }">{{ (Number(row.payedAmount) - Number(row.repayFee) - Number(row.tax)) | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.loanRate')" prop="loanInterest" width="100">
                            <template slot-scope="{ row }">{{ row.loanInterest | formatCurrency }}</template><!--利息-->
                        </el-table-column>
                        <el-table-column :label="$t('common.penaltyRate')" prop="lateCharge" width="100">
                            <template slot-scope="{ row }">{{ row.lateCharge | formatCurrency }}</template><!--罚息-->
                        </el-table-column>
                        <el-table-column :label="$t('common.lateFee')" prop="lateFee" width="100">
                            <template slot-scope="{ row }">{{ row.lateFee | formatCurrency }}</template><!--滞纳金-->
                        </el-table-column>
<!--                        <el-table-column :label="$t('common.platformCreditFee')" prop="creditServiceFee" width="100">&lt;!&ndash;平台征信费&ndash;&gt;
                            <template slot-scope="{ row }">{{ row.creditServiceFee | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('finance.platformServiceFee')" prop="platformServiceFee" min-width="120">&lt;!&ndash;平台服务费&ndash;&gt;
                            <template slot-scope="{ row }">{{ row.platformServiceFee | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('finance.accountManagementFee')" prop="accountServiceFee" min-width="120">&lt;!&ndash;账户管理费&ndash;&gt;
                            <template slot-scope="{ row }">{{ row.accountServiceFee | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('finance.riskManagementFee')" prop="riskServiceFee" min-width="120">&lt;!&ndash;风险管理费&ndash;&gt;
                            <template slot-scope="{ row }">{{ row.riskServiceFee | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('finance.beheadedInterest')" prop="headInterest" min-width="120">
                            <template slot-scope="{ row }">{{ row.headInterest | formatCurrency }}</template> &lt;!&ndash;砍头息费&ndash;&gt;
                        </el-table-column>-->
                        <el-table-column :label="$t('common.shouldRepayTime')" prop="repayTime" width="180"><!--应还款时间-->
                            <template slot-scope="{ row }">{{ row.repayTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.actRepayTime')" prop="repayYesTime" width="180"><!--实际还款时间-->
                            <template slot-scope="{ row }">{{ row.repayYesTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.overdueDays')" prop="lateDays" width="100"><!--逾期天数-->
                            <template slot-scope="{ row }">{{ row.lateDays ? (row.lateDays + $t('common.day')) : '--' }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.remitAmount')" prop="csDerateAmount" min-width="100"><!--减免金额-->
                            <template slot-scope="{ row }">{{ row.csDerateAmount | formatCurrency }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.applyDate')" prop="applyTime" width="180">
                            <template slot-scope="{ row }">{{ row.applyTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template><!--申请日期-->
                        </el-table-column>
                        <el-table-column :label="$t('common.loanCompleteTime')" prop="loanCompletionTime" width="180">
                            <template slot-scope="{ row }">{{ row.loanCompletionTime | formatDate('yyyy-MM-dd hh:mm:ss')}}</template><!--放款成功时间-->
                        </el-table-column>
<!--                        <el-table-column :label="$t('common.receiptAccount')" prop="withdrawAccount" width="150"></el-table-column>&lt;!&ndash;收款账号&ndash;&gt;-->
<!--                        <el-table-column :label="$t('common.receiptAccountBank')" prop="withdrawAccountBank" width="150"></el-table-column>&lt;!&ndash;收款账号银行&ndash;&gt;-->
                        <el-table-column :label="$t('common.userType')" prop="userType" width="150">
                             <template slot-scope="{ row }">{{ row.userType | SwitchUserType }}</template><!--用户类型-->
                        </el-table-column>
                        <el-table-column :label="$t('common.repaymentType')" prop="repayType" min-width="180"><!--放款类型-->
                            <template slot-scope="{ row }">{{ row.repayType | SwitchRepaymentType }}</template>
                        </el-table-column>

                    </el-table>
                    <!--分页组件-->
                    <pagination @pageChange="pageChange" :page="page"></pagination>
                </div><!--module_content-->
            </div>
        </div><!--module_content-->
    </div>
</template>

<script>
    import Pagination from '@/components/pagination';
    import mixin from '@/service/mixin';
    import {keyMap} from '@/config';
    import SelectAppList from '@/components/select-app-list';
    import SelectKanaList from '@/components/select-kana-list';

    export default {
        name: 'repayHasRepay',
        components: {
            Pagination,
            SelectAppList,
            SelectKanaList
        },
        mixins: [mixin],
        data() {
            return {
                domain:'',
                form: {
                    orderId: '',
                    contractNumber: '',
                    userPhone: '',
                    repayBankAccount: '',
                    userId: '',
                    applyTime: '',
                    actualRepaytime: '',
                    lateDaysType: '',
                    product: '',
                    status: '',
                    appName: '',
                    kana:'',
                    whiteProductName:'',
                    repayChannel: '',
                    repayType:'',
                    channelCompany: '',
                },
                rules: {
                    orderId: [],
                    contractNumber: [],
                    userPhone: [],
                    repayBankAccount: [],
                    userId: [],
                    applyTime: [],
                    actualRepaytime: [],
                    lateDaysType: [],
                    status: [],
                },
                payChannelList: [],
                payWayList: [],
                overdueType: keyMap.overdueType,
                keyMap
            };
        },
        methods: {
            getTableData(page = this.page) {
                const params = this.fillParams(page);
                this.$api.finance.repay.repaySuccessList(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                        this.tableData = data.resultData;
                        this.setPage(data.page, this.page);
                    }
                });
            },
            search() {
                this.page.currentPage = 1;
                this.getTableData();
            },
            exportExcel() {
                const params = this.fillParams();
                const user = this.$storage.get('user');
                const language = this.$storage.getLocal('language');
                params.language = language;
                params.token = user.token;
                const {$apiConf} = this;
                const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.finance.repay.repaySuccessExcel, ['page']);
                this.$openNewBlank(url);
            },
            fillParams(page = this.page) {
                const form = this.form;
                const params = {
                    orderId: '',
                    contractNumber: '',
                    userPhone: '',
                    repayBankAccount: '',
                    userId: '',
                    applyTimeStart: '',
                    applyTimeEnd: '',
                    actualRepaytimeStart: '',
                    actualRepaytimeEnd: '',
                    product: '',
                    status: '',
                    appName: '',
                    kana:'',
                    whiteProductName:'',
                    repayChannel: '',
                    repayType:'',
                    channelCompany: '',
                    page: {
                        pageNo: page.currentPage,
                        pageSize: this.page.pageSize
                    }
                };
                Object.keys(form).forEach(key => {
                    if (typeof form[key] === 'string' && form[key]) {
                        form[key] = form[key].trim();
                        params[key] = form[key];
                    }
                });
                if (form.lateDaysType == '3' || form.lateDaysType == '2' || form.lateDaysType == '1') {
                    params.loanExtensionTimes = form.lateDaysType;
                    params.lateDaysType = '';
                    params.loanExtensionTimess = '';
                }
                if (form.lateDaysType == '0') {
                    params.loanExtensionTimes = '';
                    params.lateDaysType = '';
                    params.loanExtensionTimess = form.lateDaysType;
                }
                if (form.lateDaysType != '3' && form.lateDaysType != '2' && form.lateDaysType != '1' && form.lateDaysType != '0' && form.lateDaysType != '') {
                    params.loanExtensionTimes = '';
                    params.lateDaysType = form.lateDaysType;
                    params.loanExtensionTimess = '';
                }
                if (form.lateDaysType == '') {
                    params.loanExtensionTimes = '';
                    params.lateDaysType = '';
                    params.loanExtensionTimess = '';
                }
                if (form.applyTime) {
                    params.applyTimeStart = form.applyTime[0];
                    params.applyTimeEnd = form.applyTime[1];
                }
                if (form.actualRepaytime) {
                    params.actualRepaytimeStart = form.actualRepaytime[0];
                    params.actualRepaytimeEnd = form.actualRepaytime[1];
                }
                return params;
            },
            getChannelPayWay(type) {
                const params = {};
                if (type === 'repayWay') {
                    params.type = 0
                }else if (type === 'repayChannel') {
                    if(this.form.channelCompany) {
                        this.form.repayChannel = '';
                        params.type = 1;
                        params.payType = 1;
                        params.channelCode = this.form.channelCompany;
                    }
                    else {
                        this.getAllChannelPayWay();
                        return;
                    }
                }
                this.$api.finance.repay.getChannelPayWay(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                      if (type === 'repayWay') {
                        this.payWayList = data || [];
                      }else if (type === 'repayChannel') {
                        this.payChannelList =  data || [];
                      }
                    }
                });
            },
            getAllChannelPayWay() {
                const params = {
                    type: 1,
                    payType: 1
                };
                this.$api.finance.productSetting.getAllChannelPayWay(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                        this.payChannelList =  data;
                    }
                });
            }
        },
        created() {
            this.domain = this.$storage.get('user').domain
            this.tableMaxHeight = document.body.clientHeight - 265;
            this.getTableData();
            this.getChannelPayWay('repayWay');
            this.getAllChannelPayWay();
        }
    }
</script>
<style lang="scss" scoped>

</style>
